﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmXuatNL_Tho : Form
    {
        string maauto;
        public frmXuatNL_Tho() { }
        public frmXuatNL_Tho(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;            
        }

        private void frmXuatNL_Tho_Load(object sender, EventArgs e)
        {
            dgvNLTho.DataSource = CTXuatKho_DB.SelectAllXuat_Tho();
            int index = 0;
            DisplayData(index);
            LoadCombobox();
            LoadComboboxManhom();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void DisplayData(int index)
        {
            txtMaXuatNL.Text = dgvNLTho.Rows[index].Cells[0].Value.ToString();
            dtpNgayThang.Text = dgvNLTho.Rows[index].Cells[1].Value.ToString();
            txtMaNX.Text = dgvNLTho.Rows[index].Cells[2].Value.ToString();
            cbbMaLo.Text = dgvNLTho.Rows[index].Cells[3].Value.ToString();
            cbbMaNhom.Text = dgvNLTho.Rows[index].Cells[4].Value.ToString();
            txtDienGiai.Text = dgvNLTho.Rows[index].Cells[5].Value.ToString();
            txtKhoiLuong.Text = dgvNLTho.Rows[index].Cells[6].Value.ToString();
            cbbKho.Text = dgvNLTho.Rows[index].Cells[7].Value.ToString();
        }

        private void dgvNLTho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                DisplayData(index);
            }
            catch (Exception)
            {

                return;
            }
            
        }

        void LoadCombobox()
        {
            cbbKho.DataSource = Kho_DB.SelectKho();
            cbbKho.DisplayMember = "tenkho";
            cbbKho.ValueMember = "makho";

            cbbMaLo.DataSource = Lo_DB.SelectLo();
            cbbMaLo.DisplayMember = "diengiai";
            cbbMaLo.ValueMember = "malo";            
        }
        void LoadComboboxManhom()
        {
            cbbMaNhom.DataSource = Nhom_DB.spSelectAllNhomByMalo(cbbMaLo.SelectedValue.ToString());
            cbbMaNhom.DisplayMember = "diengiai";
            cbbMaNhom.ValueMember = "manhom";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                CTXuatKho_DB.DeleteCTXuat_Tho(txtMaXuatNL.Text);
                frmXuatNL_Tho_Load(sender, e);
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        private void cbbMaLo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadComboboxManhom();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmXuatNL_Tho_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
            ReadOnlyTextbox();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            dtpNgayThang.Text = DateTime.Now.ToString();
            dtpNgayThang.Enabled = false;
            AutoMaXuatNLTho();
            txtMaXuatNL.Text = maauto;
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadOnlyTextbox();
                ClearTextbox();
            }
            else
            {
                try
                {
                    InsertXuatNL_Tho();
                    frmXuatNL_Tho_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtKhoiLuong.Focus();
                }
            }         
        }

        void InsertXuatNL_Tho()
        {
            CTXuatKho xuatkho = new CTXuatKho();
            xuatkho.MaXuatNL = txtMaXuatNL.Text;
            xuatkho.Ngaythang = dtpNgayThang.Text;
            xuatkho.Manx = txtMaNX.Text;
            xuatkho.Kl_xuat = Convert.ToInt32(txtKhoiLuong.Text);
            xuatkho.Malo = cbbMaLo.SelectedValue.ToString() ;
            xuatkho.Manhom = cbbMaNhom.SelectedValue.ToString();            
            xuatkho.Makho = cbbKho.SelectedValue.ToString();
            xuatkho.Diengiai = txtDienGiai.Text;
            CTXuatKho_DB.InsertCTXuat_Tho(xuatkho);
            btnThem.Text = "Thêm";
            ReadOnlyTextbox();

        }

        private void ReadOnlyTextbox()
        {
            txtKhoiLuong.ReadOnly = true;
            txtDienGiai.ReadOnly = true;
        }
        void ClearTextbox()
        {            
            txtKhoiLuong.Clear();
            txtDienGiai.Clear();
        }
        void NotReadOnlyTextbox()
        {
            txtKhoiLuong.ReadOnly = false;
            txtDienGiai.ReadOnly = false;
        }
        void AutoMaXuatNLTho()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 MaXuatNL from tblQL_XuatNL_Tho where NgayThang = '" + dtpNgayThang.Text + "' order by MaXuatNL desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayThang.Text.Replace("/", "") + "1";
            }
            else
            {
                maauto = (Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSua.Text == "Sửa")
                {
                    btnSua.Text = "Lưu";
                    NotReadOnlyTextbox();
                }
                else
                {
                    CTXuatKho xuatkho = new CTXuatKho();
                    xuatkho.MaXuatNL = txtMaXuatNL.Text;
                    xuatkho.Ngaythang = dtpNgayThang.Text;
                    xuatkho.Manx = txtMaNX.Text;
                    xuatkho.Kl_xuat = Convert.ToInt32(txtKhoiLuong.Text);
                    xuatkho.Malo = cbbMaLo.SelectedValue.ToString();
                    xuatkho.Manhom = cbbMaNhom.SelectedValue.ToString();
                    xuatkho.Makho = cbbKho.SelectedValue.ToString();
                    xuatkho.Diengiai = txtDienGiai.Text;
                    CTXuatKho_DB.UpdateCTXuat_Tho(txtMaXuatNL.Text, xuatkho);
                    btnSua.Text = "Sửa";
                    frmXuatNL_Tho_Load(sender, e);
                    ReadOnlyTextbox();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtKhoiLuong.Focus();
            }
        }
    }
}
